package tt.dz.appserver.mvc.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.avro.AvroRemoteException;
import org.apache.log4j.Logger;
//import org.mortbay.log.Log;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.base.RedisCacheDao;
import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.vo.user.UserHeadImageVo;
import tt.dz.appserver.service.business.PublicService;
import tt.dz.appserver.service.business.UserInfoService;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.RequestUtils;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.StringUtils;
import tt.dz.appserver.util.enums.Response;

/**
 * 用户业务控制层
 * 
 * @author 谢龙飞
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {
	//专门存userId和CardNo 的关联，只进不出，一般不多
	
	public static HashMap<String,String> userIdCardMap = new HashMap<String,String>();
	
	private final static Logger LOG = Logger.getLogger("[UserController]:");

	/**用户业务接口*/
	@Resource
	private UserInfoService userInfoService;
	
	/**公共业务*/
	@Resource
	private PublicService publicService;
	
	@Resource
	RedisCacheDao redisCacheDao;
	/**
	 * 用户注册
	 * @param phone	手机号码
	 * @param password	密码
	 * @param latitude	纬度
	 * @param longitude	经度
	 * @param addrdetail	详细地址
	 * @param areaCode	区号
	 * @return
	 */
	@RequestMapping(value="/registerUser",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String register(
			@RequestParam(value="phone") String phone,
			@RequestParam(value="password") String password,
			@RequestParam(required=false) String latitude,
			@RequestParam(required=false) String longitude,
			@RequestParam(value="addressDetail",required=false) String addrdetail,
			@RequestParam(value="areaCode",required=false) String areaCode) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//手机号码有误
//			if(!Validator.isMobile(phone)){
//				resultMappingEntity = Result.responseBody(Response.MOBILE_ERROR.getCode(), Response.MOBILE_ERROR.getMsg());
//				return resultMappingEntity.toString();
//			}
			
			latitude = StringUtils.getDealLatOrLng(latitude);
			longitude = StringUtils.getDealLatOrLng(longitude);
			
			//注册
			JSONObject resultObject = userInfoService.register(phone, password, latitude, longitude, addrdetail, areaCode);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if(Response.SUCCESS.getCode().equals(code)){
				//返回参数拼接（可直接删除“resultObject”对象中的code msg 之后返回对象）
				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 用户登录
	 * @param phone
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/userLogin",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String login(@RequestParam String phone,@RequestParam String password,
			@RequestParam(required=false,defaultValue="") String jPushRegId,@RequestParam(required=false)String source,
			@RequestParam(required=false)String merchantCode, 
			@RequestParam(required=false)String account,HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//手机号码有误
//			if(!Validator.isMobile(phone)){
//				resultMappingEntity = Result.responseBody(Response.MOBILE_ERROR.getCode(), Response.MOBILE_ERROR.getMsg());
//				return resultMappingEntity.toString();
//			}
			if(org.springframework.util.StringUtils.isEmpty(password)){
				resultMappingEntity = Result.responseBody(Response.NULL_PASSWORD.getCode(), Response.NULL_PASSWORD.getMsg());
				return resultMappingEntity.toString();
			}
			
			String basePath = RequestUtils.getProjectBasePath(request);
			
			JSONObject resultObject = userInfoService.userLogin(basePath, phone, password, jPushRegId,source,merchantCode);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){

				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 快速登录
	 * @param phone
	 * @param verifyCode
	 * @param terminalType
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	@RequestMapping(value="/fastLogin",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String fastLogin(
			@RequestParam String phone,
			@RequestParam(value="verifyCode") String verifyCode,
			@RequestParam(value="terminalType", required=false) String terminalType, 
			@RequestParam(required=false) String longitude, 
			@RequestParam(required=false) String latitude,
			@RequestParam(required=false,defaultValue="")  String jPushRegId){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//手机号码有误
//			if(!Validator.isMobile(phone)){
//				resultMappingEntity = Result.responseBody(Response.MOBILE_ERROR.getCode(), Response.MOBILE_ERROR.getMsg());
//				return resultMappingEntity.toString();
//			}

			//获取合法经纬度
			latitude = StringUtils.getDealLatOrLng(latitude);
			longitude = StringUtils.getDealLatOrLng(longitude);
			
			JSONObject resultObject = userInfoService.fastLogin(phone, verifyCode, latitude, longitude, terminalType, jPushRegId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){

				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 修改密码
	 * @param userId
	 * @param oldPassword
	 * @param newPassword
	 * @param confirmPassword
	 * @return
	 */
	@RequestMapping(value="/updatePassword",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String updatePassword(
			@RequestParam(value="userId") String userId,
			@RequestParam(value="historyPassword") String oldPassword, 
			@RequestParam(value="password") String newPassword,
			@RequestParam(value="confirmPassword") String confirmPassword,
			HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//验证两次密码输入
			if(!newPassword.trim().equals(confirmPassword.trim())){
				resultMappingEntity = Result.responseBody(Response.CONFIRM_PASSWORD_ERROR.getCode(), Response.CONFIRM_PASSWORD_ERROR.getMsg());
				return resultMappingEntity.toString();
			}
			
			//修改密码
			JSONObject resultObject = userInfoService.updatePassword(userId, oldPassword, newPassword);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){

				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}

	/**
	 * 重置密码，找回密码
	 * @param phone
	 * @param password
	 * @param confirmPassword
	 * @param verifyCode
	 * @return
	 */
	@RequestMapping(value="/resetPassword",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String resetPassword(String phone, String password,
			String confirmPassword,
			@RequestParam(value="verifyCode") String verifyCode,
			HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//验证两次密码输入
			if(!password.trim().equals(confirmPassword.trim())){
				resultMappingEntity = Result.responseBody(Response.CONFIRM_PASSWORD_ERROR.getCode(), Response.CONFIRM_PASSWORD_ERROR.getMsg());
				return resultMappingEntity.toString();
			}
			
			
			//重置密码
			JSONObject resultObject = userInfoService.resetPassword(phone, password, verifyCode);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//直接返回操作结果，不需要拼接新的值
			resultMappingEntity = Result.responseBody(code, msg);
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
		
	}

	/**
	 * 用户是否存在
	 * @param phone
	 * @return
	 */
	@RequestMapping(value="/userExists",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String userExists(@RequestParam String phone){

		ResultMappingEntity resultMappingEntity= null;
		try {
			//用户是否存在
			JSONObject resultJsonObject = userInfoService.userExists(phone);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");
			
			//返回结果
			resultMappingEntity = Result.responseBody(code, msg);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 更新用户信息
	 * @param userId
	 * @param nickName
	 * @param gender 性别
	 * @param addrDetail
	 * @param userImage 用户图片
	 * @param signature
	 * @param longitude
	 * @param latitude
	 * @param email
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateUserInfo",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String updateUserInfo(
			@RequestParam(value="userId") String userId,
			@RequestParam(value="nickName", required=false) String nickName,
			@RequestParam(value="sex", required=true) Integer gender,
			@RequestParam(value="address", required=false) String addrDetail,
			UserHeadImageVo userImage,
			@RequestParam(required=false) String signature,
			@RequestParam(required=false) Float longitude,
			@RequestParam(required=false) Float latitude,
			@RequestParam(required=false) String email,
			@RequestParam(required=false) String areaCode,
			@RequestParam(required=false) String carTypeId,
			HttpServletRequest request){

		ResultMappingEntity resultMappingEntity= null;
		try {
			
			String headPath = null;
			if(userImage!=null && !org.springframework.util.StringUtils.isEmpty(userImage.getSmallImage())
					&& !org.springframework.util.StringUtils.isEmpty(userImage.getMiddleImage())
					&& !org.springframework.util.StringUtils.isEmpty(userImage.getBigImage())){
				//图片处理
				headPath = JSONObject.toJSONString(userImage);
			}
			
			//修改用户信息
			JSONObject resultObject = userInfoService.updateUserInfo(userId, headPath, nickName, gender, addrDetail, longitude, latitude, email, signature,areaCode,carTypeId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if(Response.SUCCESS.getCode().equals(code)){
				
				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取用户详细信息
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getUserInfoDetail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getUserInfoDetail(@RequestParam(value="userId") String userId,
			HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//获取用户详细
			JSONObject resultObject = userInfoService.getUserInfoDetail(userId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				
				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取用户卡号
	 * @param userId
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/getUserCard",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getUserCard(@RequestParam(value="userId") String userId,
			HttpServletRequest request,HttpSession httpSession){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//获取用户详细
			JSONObject resultObject = userInfoService.getUserCardByUserId(userId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			LOG.info("code = "+code);
			if(Response.SUCCESS.getCode().equals(code)){
				
				resultObject.remove("code");
				resultObject.remove("msg");
				//直接返回操作结果，不需要拼接新的值
				JSONArray array = resultObject.getJSONArray("cardList");
				if(array!=null){
					LOG.info("array.size() = "+array.size());	
				}
				resultMappingEntity = Result.responseBody(code, msg, array);
//				LOG.info("getUserCard resultMappingEntity = " + resultMappingEntity.toString());
				
				//pengyu 20180530  从resultMappingEntity中取到
				JSONArray array1  =(JSONArray) resultObject.get("cardList");
				for (int i= 0;i<array.size();i++ ){
					Object json02 = JSONObject.parse(array.get(i).toString()); 
					Map<String,Object> jsonmap02 = (Map) json02; 
					String cardNo = jsonmap02.get("cardNo").toString();
					LOG.info("cardNo + userId:"+ cardNo +" + "+userId);
					userIdCardMap.put("cardNo_"+cardNo, userId);
					userIdCardMap.put("userId_"+userId, cardNo);
					redisCacheDao.add("cardNo_"+cardNo, userId);
					redisCacheDao.add("userId_"+userId, cardNo);
					}
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 用户余额查询
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getUserBalance",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getUserWallet(@RequestParam(value="cardNo") String cardNo,
			HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//获取用户详细
			JSONObject resultObject = userInfoService.getUserWallet(cardNo);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//如果消息为正确的
			if(Response.SUCCESS.getCode().equals(code)){
				
				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 校验token
	 * @param userId
	 * @param token
	 * @param jpushRegId
	 * @return
	 */
	@RequestMapping(value="/checkOutToken",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String checkOutToken(@RequestParam String token,@RequestParam String jPushRegId){

		ResultMappingEntity resultMappingEntity= null;
		try {
			//获取用户详细
			JSONObject resultObject = userInfoService.checkOutToken(token, jPushRegId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//如果消息为正确的
			if(Response.SUCCESS.getCode().equals(code)){
				
				Object object = resultObject.get("userDetailVo");
				if(object==null){
					resultMappingEntity = Result.responseBody(Response.USER_NOT_FOUND.getCode(), Response.USER_NOT_FOUND.getMsg());
				}
				else{
					resultObject.remove("code");
					resultObject.remove("msg");
					resultMappingEntity = Result.responseBody(code, msg, object);
				}
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	/**
	 * 自动登录
	 * @param source 系统码
	 * @param merchantCode 运营商码
	 * @param account  运营商对应的用户
	 * @param jPushRegId 用户对应的唯一标识
	 * @return
	 */
	@RequestMapping(value="/autoLogin",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String autoLogin(@RequestParam String source,
			@RequestParam(value="merchantCode") String merchantCode,
			@RequestParam(value="account") String account, 
			@RequestParam(value="jPushRegId", required=false,defaultValue="") String jPushRegId){
		
		ResultMappingEntity resultMappingEntity= null;
		try{
			
			JSONObject resultObject = userInfoService.autoLogin(source,merchantCode,account,jPushRegId);
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){

				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/unlockUser",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String unlockUser(@RequestParam String source,
			@RequestParam(value="merchantCode") String merchantCode,
			@RequestParam(value="account") String account, 
			@RequestParam(value="jPushRegId", required=false) String jPushRegId ,
			@RequestParam(value="bindType", required=false) String bindType ){
		
		ResultMappingEntity resultMappingEntity= null;		
		try {
			JSONObject resultObject = userInfoService.unlockUser(source,merchantCode,account,jPushRegId,bindType);
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody("0", msg);
			}		
		} catch (AvroRemoteException e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		return resultMappingEntity.toString();
	}
	
	
	
	@RequestMapping(value="/noLoginBindingUser",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String noLoginBindingUser( String phone, String verifyCode,
			@RequestParam(value = "source",required=false,defaultValue="") String source,
			@RequestParam(value = "merchantCode",required=false,defaultValue="") String merchantCode,
			@RequestParam(value = "account",required=false,defaultValue="") String account,
			@RequestParam(value = "bindingType",required=false,defaultValue="1") String  bindingType ,
			String  nickname,String sex, @RequestParam(value="address", required=false) String addrDetail,
			UserHeadImageVo userImage
			){
		
		String headPath = null;
		if(userImage!=null && !org.springframework.util.StringUtils.isEmpty(userImage.getSmallImage())
				&& !org.springframework.util.StringUtils.isEmpty(userImage.getMiddleImage())
				&& !org.springframework.util.StringUtils.isEmpty(userImage.getBigImage())){
			//图片处理
			headPath = JSONObject.toJSONString(userImage);
		}
		if(source==null){
			source=Constants.systemCode;
		}
		if(merchantCode==null){
			merchantCode=Constants.merchantCode;
		}
		ResultMappingEntity resultMappingEntity= null;		
		try {
			JSONObject resultObject = userInfoService.bindingUser(phone,verifyCode,source,merchantCode,
					account,bindingType,nickname,sex,addrDetail,headPath);
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody("0", msg);
			}		
		} catch (AvroRemoteException e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/loginBindingUser",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String loginBindingUser( String phone, String userId,
			@RequestParam(value = "account",required=false,defaultValue="") String account,
			@RequestParam(value = "bindingType",required=false,defaultValue="1") String  bindingType ,
			String source, String merchantCode
			){
		
		if(source==null){
			source=Constants.systemCode;
		}
		if(merchantCode==null){
			merchantCode=Constants.merchantCode;
		}
		ResultMappingEntity resultMappingEntity= null;		
		try {
			JSONObject resultObject = userInfoService.loginBindingUser(phone,userId,account,bindingType,source,merchantCode);
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			//如果接口为成功
			if(Response.SUCCESS.getCode().equals(code)){
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("userDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody("0", msg);
			}		
		} catch (AvroRemoteException e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value="/getWeixinDetail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String unlockUser(@RequestParam(value="openid")String openid,@RequestParam(value="access_token")String access_token){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//获取用户详细
			JSONObject resultObject = userInfoService.getWeixinDetail(openid,access_token);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//如果消息为正确的
			if(Response.SUCCESS.getCode().equals(code)){
				
				resultObject.remove("code");
				resultObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultObject.get("data"));
			}
			else{
				//直接返回操作结果，不需要拼接新的值
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 重置密码，找回密码
	 * @param phone
	 * @param password
	 * @param confirmPassword
	 * @param verifyCode
	 * @return
	 */
	@RequestMapping(value="/setPassword",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String setPassword(String phone, String password,
			String confirmPassword,
			@RequestParam(value="type") String type,
			@RequestParam(value="merchantCode" ,required=false ,defaultValue="") String merchantCode,String userId,
			HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//验证两次密码输入
			if(!password.trim().equals(confirmPassword.trim())){
				resultMappingEntity = Result.responseBody(Response.CONFIRM_PASSWORD_ERROR.getCode(), Response.CONFIRM_PASSWORD_ERROR.getMsg());
				return resultMappingEntity.toString();
			}
			
			
			//重置密码
			JSONObject resultObject = userInfoService.setPassword(phone, password, type,merchantCode,userId);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//直接返回操作结果，不需要拼接新的值
			resultMappingEntity = Result.responseBody(code, msg);
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
		
	}
	
	/**
	 * 获取绑定列表
	 * @param phone 用户手机号或者，用户id
	 * @param bindType 绑定类型
	 * @param merchantCode 运营商
	 * @param source  系统吗
	 * @return
	 */
	@RequestMapping(value="/bindShipList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String bindShipList(String phone, @RequestParam(defaultValue="")String bindType,
			String merchantCode,@RequestParam(value="source") String source,HttpServletRequest request){
		
		ResultMappingEntity resultMappingEntity= null;
		try {
			
			//重置密码
			JSONObject resultObject = userInfoService.bindShipList(phone, bindType,source,merchantCode);
			
			//获取编码和消息
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");

			//直接返回操作结果，不需要拼接新的值
			resultMappingEntity = Result.responseBody(code, msg,resultObject.get("data"));
			
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
		
		
	}
	
	public static void main(String args[]) {
//		Object json02 = JSONObject.parse(array.get(i).toString()); 
//		Map<String,Object> jsonmap02 = (Map) json02; 
//		LOG.info("array1.size() = "+array1.size());	
//		String cardNo = jsonmap02.get("cardNo").toString();
//		session.setAttribute(cardNo, userId);
//		session.setAttribute(userId, cardNo);	
//		LOG.info("cardNo + userId:"+ cardNo +" + "+userId);
		
//		Object json02 = JSONObject.parse("{\"cardList\":[{\"cardNo\":\"18808002810000004354\",\"state\":1,\"cardType\":\"08\"}],\"isSuccess\":1}"); 
		Object json02 = JSONObject.parse("{\"cardNo\":\"18808002810000004354\",\"state\":1,\"cardType\":\"08\"}"); 
		Map<String,Object> jsonmap02 = (Map) json02; 
		String cardNo = jsonmap02.get("cardNo").toString();
		System.out.println("cardNo :"+ cardNo );
		
		
		
		
	}
}
